/*	file :	liste.c	*/
/*	created by :	moya_a	*/
/*	date :	Wed Apr  6 19:54:42 CEST 2011	*/

#include <stdlib.h>
#include <unistd.h>

#include "liste.h"

struct s_liste
{
    t_som       data;
    t_liste     next;
};

static inline t_liste next (t_liste lst)
{
    return lst->next;
}

int is_empty(t_liste lst)
{
    return lst?0:1;
}

t_liste ajouter (t_som d, t_liste lst)
{
    t_liste             new = NULL, tmp = NULL, keep;
    keep = lst;
    new = malloc (sizeof(struct s_liste));
    new->data = d;
    new->next = NULL;
    for (;lst && d->cost > lst->data->cost; lst = next(lst));
    if (lst)
    {
        tmp = lst->next;
        lst->next = new;
        new->next = tmp;
    }
    else
    {
        keep = new;
    }
    return keep;
}

int belongto(t_som ps, t_liste lst)
{
    t_liste             tmp = NULL;
    tmp = lst;

    for (; lst; lst = next(lst))
    {
        if (lst->data == ps)
            return 1;
    }
    return 0;
}

t_som take_min (t_liste *lst)
{
    t_som               data;
    t_liste             tmp;
    data = (*lst)->data;
    tmp = *lst;
    *lst = next(*lst);
    free (tmp);
    return data;
}

